主题
查找图片4 - FindImagePtrFromPath
函数简介
在指定内存源图中查找指定图像,源图通过内存指针传入,模板通过文件路径指定,返回第一个匹配结果。
接口名称
FindImagePtrFromPathDLL调用
long FindImagePtrFromPath(long instance, long source, string templ, string deltaColor, double matchVal, int dir);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| source | 长整数型 | 源图句柄,由 LoadImage 等接口生成。 |
| templ | 字符串 | 模板图片的路径,可用 | 分隔多图,如 "test.bmp|test2.bmp|test3.bmp"。默认任一模板不存在或加载失败时整次返回空;可通过 SetConfigByKey 设置 MultiTemplateAbortOnInvalidImage=false 跳过无效模板继续匹配。 |
| deltaColor | 字符串 | 颜色差值,格式为"RRGGBB",如"101010"。 |
| matchVal | 双精度浮点数 | 相似度,如0.85,最大为1。 |
| dir | 整数型 | 查找方向:0-从左到右从上到下;1-从左到右从下到上;2-从右到左从上到下;3-从右到左从下到上;4-从中心往外查找;5-从上到下从左到右;6-从上到下从右到左;7-从下到上从左到右;8-从下到上从右到左。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
MatchData result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState) {
int x = result.X;
int y = result.Y;
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0)
{
var result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState)
{
int x = result.X;
int y = result.Y;
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
img_ptr = ola.LoadImage("images/scene.png")
if img_ptr:
result = ola.FindImagePtrFromPath(img_ptr, "img/btn_ok.bmp", "101010", 0.9, 0)
if result.get("MatchState"):
x = result["X"]
y = result["Y"]
# 后续不再使用该图时释放
ola.FreeImage(img_ptr)java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;
OLAPlugServer ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
MatchResult result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result != null && result.MatchState) {
int x = result.X;
int y = result.Y;
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr);
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
imgPtr := ola.LoadImage("images/scene.png")
if imgPtr != 0 {
MatchData result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0)
if result.MatchState {
x := result.X
y := result.Y
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr)
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let imgPtr = ola.load_image("images/scene.png");
if imgPtr != 0 {
MatchData result = ola.find_image_ptr_from_path(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0)
if result.MatchState {
let x = result.X;
let y = result.Y;
}
// 后续不再使用该图时释放
ola.free_image(imgPtr);
}cpp
var ola = com("OlaPlug.OlaSoft")
var imgPtr = ola.LoadImage("images/scene.png")
if(imgPtr) {
var result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0)
if(result.MatchState) {
var x = result.X
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
imgPtr = ola.LoadImage("images/scene.png")
If imgPtr <> 0 Then
Set result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0)
If result.MatchState Then
' 后续不再使用该图时释放
ola.FreeImage(imgPtr)
End Iftext
.局部变量 ola, OLAPlug
.局部变量 result, MatchData
.局部变量 rawstring, 文本型
.局部变量 imgPtr, 长整数型
ola.创建 ()
imgPtr = ola.LoadImage ("images/scene.png")
.如果真 (imgPtr ≠ 0)
result = ola.FindImagePtrFromPath (imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0, rawstring)
' rawstring 为原始 JSON 文本(可空,可省略);仅在需要 ParseMatchImageJson / SortPosDistance 等时再传
.如果真 (result.MatchState)
.如果真结束
ola.FreeImage (imgPtr)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var imgPtr = ola.LoadImage("images/scene.png");
if(imgPtr){
var result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if(result.MatchState){
var x = result.X;
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr);
}text
变量 ola <类型 = OLAPlugServer>
变量 result <类型 = MatchData>
变量 imgPtr <类型 = 长整数>
ola = 新建 OLAPlugServer
imgPtr = ola.LoadImage("images/scene.png")
如果真 (imgPtr ≠ 0)
result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0)
如果真 (result.MatchState)
ola.FreeImage(imgPtr)
结束cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
MatchData result = ola.FindImagePtrFromPath(imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState) {
int32_t x = result.X;
int32_t y = result.Y;
}
// 后续不再使用该图时释放
ola.FreeImage(imgPtr);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long ptr = FindImagePtrFromPath(instance, imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (ptr != 0) {
int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
char json[4096] = {0};
GetStringFromPtr(ptr, json, sizeof(json));
ParseMatchImageJson(instance, json, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
FreeStringPtr(instance, ptr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long FindImagePtrFromPath(long ola, long source, string templ, string deltaColor, double matchVal, int dir);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageJson(string str, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);
long instance = CreateCOLAPlugInterFace();
long ptr = FindImagePtrFromPath(instance, imgPtr, "img/btn_ok.bmp", "101010", 0.9, 0);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
ParseMatchImageJson(sb.ToString(), out int matchState, out int x, out int y, out int w, out int h, out double mv, out double ang, out int idx);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.FindImagePtrFromPath(instance, ...)
# 原生返回 JSON 字符串指针,可用 ParseMatchImageJson 解析返回值
| 调用方式 | 返回值 | 说明 |
|---|---|---|
| SDK | MatchData(C#/Java 为 MatchResult;Python 为 dict) | 结构化匹配结果;MatchState 为真表示匹配成功 |
| SDK(易语言/火山) | MatchData | 内部已通过 GetStringFromPtr + OL_获取MatchData 解析 |
| 原生 DLL | 非 0 | 成功,返回 JSON 字符串指针,需调用 FreeStringPtr 释放 |
| 原生 DLL | 0 | 失败 |
返回数据格式:
json
{
"MatchVal": 0.85,
"MatchState": true,
"Index": 0,
"X": 100,
"Y": 200,
"Width": 100,
"Height": 100
}| 字段名 | 类型 | 说明 |
|---|---|---|
| MatchVal | 浮点数 | 匹配相似度。 |
| MatchState | 布尔 | 是否匹配成功。 |
| Index | 整数 | 结果索引(从 0 开始)。 |
| X | 整数 | X 坐标。 |
| Y | 整数 | Y 坐标。 |
| Width | 整数 | 宽度。 |
| Height | 整数 | 高度。 |
相关配置
| 配置项 | 类型 | 默认 | 说明 |
|---|---|---|---|
| MultiTemplateAbortOnInvalidImage | bool | true | 多模板路径 | 分隔时,某一图不存在或加载失败:true(默认)整次返回空;false 跳过无效模板继续匹配(全局配置,见 SetConfigByKey)。 |
